package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.system.domain.WdyRecharge;
import com.ruoyi.system.domain.WdyUser;
import com.ruoyi.system.domain.WdyVcRelease;
import com.ruoyi.system.service.IWdyUserService;
import com.ruoyi.system.service.IWdyVcReleaseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * vc释放 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-12-16
 */
@Controller
@RequestMapping("/system/wdyVcRelease")
public class WdyVcReleaseController extends BaseController
{
    private String prefix = "system/wdyVcRelease";
	
	@Autowired
	private IWdyVcReleaseService wdyVcReleaseService;
	@Autowired
	private IWdyUserService wdyUserService;

	@RequiresPermissions("system:wdyVcRelease:view")
	@GetMapping()
	public String wdyVcRelease()
	{
	    return prefix + "/wdyVcRelease";
	}
	
	/**
	 * 查询vc释放列表
	 */
	@RequiresPermissions("system:wdyVcRelease:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(WdyVcRelease wdyVcRelease)
	{
		startPage();
        List<WdyVcRelease> list = wdyVcReleaseService.selectWdyVcReleaseList(wdyVcRelease);
		for(WdyVcRelease e : list) {
			QueryWrapper<WdyUser> query = new QueryWrapper<>();
			query.eq("user_id", e.getUserId());
			WdyUser user = wdyUserService.getOne(query);
			if(user != null) {
				e.addExtraData("mobile", user.getUserName());
			}
		}
		return getDataTable(list);
	}
	
	
	/**
	 * 导出vc释放列表
	 */
	@RequiresPermissions("system:wdyVcRelease:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WdyVcRelease wdyVcRelease)
    {
    	List<WdyVcRelease> list = wdyVcReleaseService.selectWdyVcReleaseList(wdyVcRelease);
        ExcelUtil<WdyVcRelease> util = new ExcelUtil<WdyVcRelease>(WdyVcRelease.class);
        return util.exportExcel(list, "wdyVcRelease");
    }
	
	/**
	 * 新增vc释放
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存vc释放
	 */
	@RequiresPermissions("system:wdyVcRelease:add")
	@Log(title = "vc释放", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(WdyVcRelease wdyVcRelease)
	{		
		return toAjax(wdyVcReleaseService.insertWdyVcRelease(wdyVcRelease));
	}

	/**
	 * 修改vc释放
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		WdyVcRelease wdyVcRelease = wdyVcReleaseService.selectWdyVcReleaseById(id);
		mmap.put("wdyVcRelease", wdyVcRelease);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存vc释放
	 */
	@RequiresPermissions("system:wdyVcRelease:edit")
	@Log(title = "vc释放", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(WdyVcRelease wdyVcRelease)
	{		
		return toAjax(wdyVcReleaseService.updateWdyVcRelease(wdyVcRelease));
	}
	
	/**
	 * 删除vc释放
	 */
	@RequiresPermissions("system:wdyVcRelease:remove")
	@Log(title = "vc释放", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(wdyVcReleaseService.deleteWdyVcReleaseByIds(ids));
	}

	@RequiresPermissions("system:wdyVcRelease:view")
	@PostMapping("/getSum")
	@ResponseBody
	public AjaxResult getSum(WdyVcRelease wdyVcRelease)
	{
		AjaxResult success = success();
		BigDecimal sum = wdyVcReleaseService.getSum(wdyVcRelease);
		return success.put("sum", sum);
	}
}
